# encoding = utf-8

import sys
import time
import pygame
from pygame.sprite import Group

from snake import Snake
from food import Food

def eat_check(food,snake,score,screen,snake_g):
    for f in food.copy():
        #假设矩形坐标分别为[（f.rect.x,f.rect.y）,(f.rect.x+10,f.rect.y+10)]
        #[（snake.rect.x,snake.rect.y）,(snake.rect.x+10,snake.rect.y+10)]相交的话两个点坐标分别为
        #max[（f.rect.x,f.rect.y）,（snake.rect.x,snake.rect.y）],min[(f.rect.x+10,f.rect.y+10),(snake.rect.x+10,snake.rect.y+10)]
        minx = max(f.rect.x,snake.rect.x)
        miny = max(f.rect.y,snake.rect.y)
        maxx = min((f.rect.x+10),(snake.rect.x+10))
        maxy = min((f.rect.y+10),(snake.rect.y+10))
        #不相交则进入下一次循环
        if minx >= maxx or miny >= maxy:
            pass
        else:
            food.remove(f)
            score += 1
            snake_t = Snake(screen)
            snake_g.append(snake_t)
            print("*********加1分***********")
        
    
def game_init(snake,food,screen):
    #初始化一条蛇
    snake_first = Snake(screen)
    snake.append(snake_first)
    #初始化食物
    food_g = Group()
    for i in range(20):
        food_t = Food(screen)
        food.append(food_t)
        food_g.add(food_t)
        

def run_game():

     #初始化游戏并创建一个屏幕对象
    pygame.init()
    screen = pygame.display.set_mode((800,800))
    pygame.display.set_caption("GreedySnake")
    snake = []
    food = []
    game_init(snake,food,screen)
    #初始化方向
    direction = 'up'
    #初始化分数
    score = 0
    #开始游戏的主循环
    while  True:
        #监视键盘和鼠标事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT and direction != 'left':
                    direction = 'right'
                elif event.key == pygame.K_LEFT and direction != 'right':
                    direction = 'left'
                elif event.key == pygame.K_UP and direction != 'down':
                    direction = 'up'
                elif event.key == pygame.K_DOWN and direction != 'up':
                    direction = 'down'
                    #print("hello")

        
        print(snake[0].centery,snake[0].centerx)
        
        for n in range(len(snake)-1,0,-1):
            snake[n].rect.centerx =  snake[n-1].rect.centerx
            snake[n].rect.centery =  snake[n-1].rect.centery
            
     
            
        print("------------------------------------------")
        #snake[0].update(direction)
        #snake[1].rect.top = snake[0].rect.bottom
        snake[0].update(direction)
       
        #snake[1].update(direction)
        #snake_first.update(direction)
        #检测碰撞
        eat_check(food,snake[0],score,screen,snake)

        #重新填充屏幕
        screen.fill((125,125,125))
        #重新画蛇
        for s in snake:
            s.blitme()
        print("蛇的长度为:" + str(len(snake)))
        #重新画剩余的食物
        for f in food:
            f.showme()
        print("剩余的食物数量："+str(len(food)))
        
        
        #snake.draw(screen)
        #让最近绘制的屏幕可见
        pygame.display.flip()
        
        if not snake[0].state:
            print(snake[0].state)
            print(snake[0].rect.bottom)
            print(screen.get_rect().bottom)
            break
        
        #time.sleep(5)
run_game()
